package college.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;



public class ExcelFileGenerator {

	public ExcelFileGenerator() {
		super();

	}

	public static void createExcelFile(String fileName, String[] headings,
			Object[][] data) {

		HSSFWorkbook book = new HSSFWorkbook();
		HSSFSheet sheet = book.createSheet("Attendance");

		HSSFCellStyle style = book.createCellStyle();
		Font font = book.createFont();
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		font.setColor(Font.COLOR_NORMAL);

		style.setFont(font);
		// style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		style.setFillBackgroundColor(HSSFColor.BLUE.index);
		HSSFRow[] rows = new HSSFRow[data.length + 1];

		for (int i = 0; i < rows.length; i++) {
			rows[i] = sheet.createRow(i);
			HSSFCell[] cells = new HSSFCell[headings.length];
			int c = 0;

			for (int j = 0; j < cells.length; j++) {
				cells[j] = rows[i].createCell(c++);

				if (i == 0) {
					cells[j].setCellValue(headings[j]);
					cells[j].setCellStyle(style);
				} else {
					//Student student = data.[i - 1][j];
					cells[j].setCellValue(data[i - 1][j].toString());
					//cells[j].setCellValue(student.getBranch());
					
				}
			}

			
		}
		FileOutputStream output = null;
		try {
			output = new FileOutputStream(fileName);
			book.write(output);
			output.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				output.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
